package com.chan.net;

import android.net.ParseException;
import android.text.TextUtils;

import com.google.gson.JsonParseException;

import org.json.JSONException;

import java.io.InterruptedIOException;
import java.net.ConnectException;
import java.net.UnknownHostException;

import io.reactivex.Observer;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import retrofit2.HttpException;

import static com.chan.net.DefaultObserver.ExceptionReason.CONNECT_ERROR;
import static com.chan.net.DefaultObserver.ExceptionReason.CONNECT_TIMEOUT;
import static com.chan.net.DefaultObserver.ExceptionReason.PARSE_ERROR;
import static com.chan.net.DefaultObserver.ExceptionReason.UNKNOWN_ERROR;

public class DefaultObserver implements Observer<Result> {

    private HttpCallback httpCallback;
    private String requestCommand;

    public DefaultObserver(String requestCommand, HttpCallback httpCallback) {
        this.httpCallback = httpCallback;
        this.requestCommand = requestCommand;
    }

    @Override
    public void onSubscribe(@NonNull Disposable d) {

    }

    @Override
    public void onNext(@NonNull Result t) {
        dismissProgress();
        if (t.code == 0) {
            if (t.data != null) {
                if (httpCallback != null) {
                    httpCallback.onSuccess(requestCommand, t);
                }
                return;
            }
        }
        if (httpCallback != null) {
            httpCallback.onFail(requestCommand, t);
        }
    }

    private void dismissProgress() {

    }


    @Override
    public void onError(@NonNull Throwable e) {
        dismissProgress();
        if (e instanceof HttpException) {     //   HTTP错误
            onException(ExceptionReason.BAD_NETWORK);
        } else if (e instanceof ConnectException
                || e instanceof UnknownHostException) {   //   连接错误
            onException(CONNECT_ERROR);
        } else if (e instanceof InterruptedIOException) {   //  连接超时
            onException(CONNECT_TIMEOUT);
        } else if (e instanceof JsonParseException
                || e instanceof JSONException
                || e instanceof ParseException) {   //  解析错误
            onException(PARSE_ERROR);
        } else {
            onException(UNKNOWN_ERROR);
        }
    }

    @Override
    public void onComplete() {

    }

    /**
     * 服务器返回数据，但响应码不为200
     *
     * @param response 服务器返回的数据
     */
    public void onFail(Result response) {
        String message = response.getMessage();
        if (TextUtils.isEmpty(message)) {
            //ToastUtils.show(R.string.response_return_error);
        } else {
            //ToastUtils.show(message);
        }
    }

    /**
     * 请求异常
     *
     * @param reason
     */
    public void onException(ExceptionReason reason) {
        switch (reason) {
            case CONNECT_ERROR:
                //ToastUtils.show(R.string.connect_error, Toast.LENGTH_SHORT);
                break;

            case CONNECT_TIMEOUT:
                //ToastUtils.show(R.string.connect_timeout, Toast.LENGTH_SHORT);
                break;

            case BAD_NETWORK:
                //ToastUtils.show(R.string.bad_network, Toast.LENGTH_SHORT);
                break;

            case PARSE_ERROR:
                if (httpCallback != null) {
                    httpCallback.onFail(requestCommand, null);
                }
                //ToastUtils.show(R.string.parse_error, Toast.LENGTH_SHORT);
                break;

            case UNKNOWN_ERROR:
            default:
                //ToastUtils.show(R.string.unknown_error, Toast.LENGTH_SHORT);
                break;
        }
    }

    /**
     * 请求网络失败原因
     */
    public enum ExceptionReason {
        /**
         * 解析数据失败
         */
        PARSE_ERROR,
        /**
         * 网络问题
         */
        BAD_NETWORK,
        /**
         * 连接错误
         */
        CONNECT_ERROR,
        /**
         * 连接超时
         */
        CONNECT_TIMEOUT,
        /**
         * 未知错误
         */
        UNKNOWN_ERROR
    }
}
